package com.wc.AlgoOJ_homework.AlgoOJ_test6.I_熊二最喜欢的问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/22 22:22
 * @description http://43.138.190.70:8888/p/744?tid=65f90101f9a80dc9ddc3f0c4
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = N << 1;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] w = new int[M];
    static int[] ne = new int[M];
    static boolean[] st = new boolean[N];
    static int idx = 1;
    static boolean isSuccess;
    static int t, n, A, B, a, b, c;
    static int[] aTo = new int[N];
    static int[] bTo = new int[N];

    public static void main(String[] args) {
        t = sc.nextInt();
        while (t-- > 0) {
            idx = 1;
            n = sc.nextInt();
            A = sc.nextInt();
            B = sc.nextInt();
            for (int i = 2; i <= n; i++) {
                a = sc.nextInt();
                b = sc.nextInt();
                c = sc.nextInt();
                add(a, b, c);
                add(b, a, c);
            }

            isSuccess = false;
            Arrays.fill(aTo, 1, n + 1, -1);
            Arrays.fill(bTo, 1, n + 1, -1);
            dfs(A, 0, aTo);
            dfs(B, 0, bTo);
            Set<Integer> set = new HashSet<>();
            for (int i = 1; i <= n; i++) {
                if (aTo[i] != -1 && i != B && i != A) set.add(aTo[i]);
            }
            for (int i = 1; i <= n; i++) {
                if (bTo[i] != -1 && i != B && i != A && set.contains(bTo[i])) {
                    isSuccess = true;
                    break;
                }
            }
            out.println(isSuccess || aTo[B] == 0 ? "YES" : "NO");
            Arrays.fill(h, 1, n + 1, 0);
            Arrays.fill(e, 1, (n + 1) << 1, 0);
            Arrays.fill(w, 1, (n + 1) << 1, 0);
            Arrays.fill(ne, 1, (n + 1) << 1, 0);
        }
        out.flush();
    }

    static void dfs(int u, int x, int[] aTo) {
        st[u] = true;
        for (int i = h[u]; i != 0; i = ne[i]) {
            int j = e[i];
            aTo[j] = x ^ w[i];
            if (!st[j]) dfs(j, x ^ w[i], aTo);
        }

        st[u] = false;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        w[idx] = c;
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
